package cn.cxion.chat.app.view.popupview;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ToastUtils;
import com.lqr.optionitemview.OptionItemView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.AppointmentInfo;
import cn.cxion.chat.app.bean.AppointmentTimes;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.StringUtil;

import static com.lxj.xpopup.enums.PopupAnimation.TranslateFromBottom;

public class DatePickPopupView extends CenterPopupView {


    @Bind(R.id.datepicke)
    DatePicker datepicke;
    @Bind(R.id.op_time)
    OptionItemView opTime;
    @Bind(R.id.cancle)
    TextView cancle;
    @Bind(R.id.ok)
    TextView ok;
    private String time;
    DateSelectListener listener;
    AppointmentTimes appointmentTimes;

    public DatePickPopupView(@NonNull Context context) {
        super(context);
    }


    public DatePickPopupView(@NonNull Context context, AppointmentTimes appointmentTimes) {
        super(context);
        if (appointmentTimes != null) {
            LogUtil.print("appointmentTimes != null");
            this.appointmentTimes = appointmentTimes;
        } else {
            this.appointmentTimes = new AppointmentTimes();
//            //初始化时间
//            this.appointmentTimes.setTimeText(StringUtil.getTimeHM());
//            this.appointmentTimes.setHour(Integer.valueOf(StringUtil.getTimeH()));
//            this.appointmentTimes.setMinute(Integer.valueOf(StringUtil.getTimeM()));
        }
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_datepicker;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        if (!StringUtil.isEmpty(appointmentTimes.getHourText())) {
            time =appointmentTimes.getHourText();
        } else {
            time = StringUtil.getTimeHM();
            appointmentTimes.setHourText(StringUtil.getTimeHM());
            appointmentTimes.setHour(Integer.valueOf(StringUtil.getTimeH()));
            appointmentTimes.setMinute(Integer.valueOf(StringUtil.getTimeM()));
        }
        if(!StringUtil.isEmpty(appointmentTimes.getTimeText())) {
             datepicke.init(appointmentTimes.getYear(),appointmentTimes.getMonth(),appointmentTimes.getDay(),null);
        }
        opTime.setLeftText(time);
    }


    public void setOnDateSelectListener(DateSelectListener listener) {
        this.listener = listener;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onShow() {
        super.onShow();
        ButterKnife.bind(this);

    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }


    @OnClick({R.id.op_time, R.id.cancle, R.id.ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.op_time:
                 TimePickPopupView timePickPopupView=new TimePickPopupView(getContext(),appointmentTimes);
                 timePickPopupView.setOnTimeSelectListener(new TimePickPopupView.OnTimeSelectListener() {
                     @Override
                     public void select(AppointmentTimes appointmentTimes) {
                         opTime.setLeftText(appointmentTimes.getHourText());
                     }
                 });
                new XPopup.Builder(getContext())
                        .popupAnimation(TranslateFromBottom)
                        .asCustom(timePickPopupView/*.enableDrag(false)*/)
                        .show();
                break;
            case R.id.cancle:
                dismiss();
                break;
            case R.id.ok:
                appointmentTimes.setYear(datepicke.getYear());
                appointmentTimes.setMonth(datepicke.getMonth());
                appointmentTimes.setDay(datepicke.getDayOfMonth());
                appointmentTimes.setTimeText(datepicke.getYear()+"-"+StringUtil.addZero((datepicke.getMonth()+1))+"-"+datepicke.getDayOfMonth()+" "+appointmentTimes.getHourText());
                listener.selectDate(appointmentTimes);
                dismiss();
                break;
        }
    }

    public interface DateSelectListener {
        void selectDate(AppointmentTimes appointmentTimes);
    }

    private void hideDatePickerHeader(DatePicker datePicker) {
        ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);
        if (rootView == null) {
            return;
        }
        View headerView = rootView.getChildAt(0);
        if (headerView == null) {
            return;
        }
        //5.0+
        int headerId = getContext().getResources().getIdentifier("day_picker_selector_layout", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
            ViewGroup.LayoutParams layoutParamsRoot = rootView.getLayoutParams();
            layoutParamsRoot.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            rootView.setLayoutParams(layoutParamsRoot);
            ViewGroup animator = (ViewGroup) rootView.getChildAt(1);
            ViewGroup.LayoutParams layoutParamsAnimator = animator.getLayoutParams();
            layoutParamsAnimator.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            animator.setLayoutParams(layoutParamsAnimator);
            View child = animator.getChildAt(0);
            ViewGroup.LayoutParams layoutParamsChild = child.getLayoutParams();
            layoutParamsChild.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            child.setLayoutParams(layoutParamsChild);
            return;
        }
        //6.0+
        headerId = getContext().getResources().getIdentifier("date_picker_header", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
        }
    }
}
